package com.ouyu.factory.support;

import cn.hutool.core.bean.BeanUtil;
import com.ouyu.PropertyValue;
import com.ouyu.PropertyValues;
import com.ouyu.factory.config.BeanDefinition;
import com.ouyu.factory.config.BeanReference;
import com.ouyu.factory.exception.BeansException;

import java.lang.reflect.Constructor;

/**
 * @Author: hz21056677
 * @Description:
 * @Date: Created in 17:34 2021/6/3
 */
public abstract class AbstractAutoWireCapableBeanFactory extends AbstractBeanFactory{
    //选择的cglib 实例化对象
    private InstantiationStrategy instantiationStrategy = new CglibSubclassInstantiationStrategy();
    @Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) {
        Object bean = null;
        try {
           bean = createBeanInstance(beanDefinition,beanName,args);
           //给bean进行属性填充
           applyPropertyValues(beanName,bean,beanDefinition);
        } catch (Exception e) {
           throw new BeansException("instantiation of bean failed",e);
        }
        addSingleton(beanName,bean);
        return bean;
    }

    /**
     * Bean属性填充
     * @param beanName bean的名称
     * @param bean bean
     * @param beanDefinition bean的定义
     */
    private void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
        try {
            PropertyValues propertyValues = beanDefinition.getPropertyValues();
            for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
                String name = propertyValue.getName();
                Object value = propertyValue.getValue();
                if(value instanceof BeanReference){
                    //A依赖B，获取B的实例化
                     BeanReference beanReference = (BeanReference) value;
                     value = getBean(beanReference.getBeanName());
                }

                BeanUtil.setFieldValue(bean, name, value);
            }
        } catch (Exception e) {
            throw new BeansException("Error setting property values: "+beanName);
        }
    }

    protected  Object createBeanInstance(BeanDefinition definition, String beanName, Object[] args){
        Constructor constructor = null;
        Class<?> beanClass = definition.getBeanClass();
        final Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
        for (Constructor<?> ctor : declaredConstructors) {
            if (null != args && ctor.getParameterTypes().length == args.length) {
                constructor = ctor;
                break;
            }
        }
        return getInstantiationStrategy().instantiate(definition,beanName,constructor,args);
    }
    public InstantiationStrategy getInstantiationStrategy(){
        return instantiationStrategy;
    }

    /**
     *  set新的实例化策略
     * @param instantiationStrategy 实例化策略
     */
    public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
        this.instantiationStrategy = instantiationStrategy;
    }
}
